//
//  FPLuckyDrawAlertView.m
//  FootPad
//
//  Created by 张浩 on 2021/5/28.
//  Copyright © 2021 hb_147. All rights reserved.
//

#import "MM_CodeResultPicView.h"
#import "AppDelegate.h"
#import "MM_UploadPicView.h"
#import "SDPhotoBrowser.h"

static const NSInteger maxCountOfRow = 6;
static const NSInteger AddTag = 100000;

#define max_X 80

@interface MM_CodeResultPicView ()<SDPhotoBrowserDelegate>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIView *picView;
@property (nonatomic, strong) UIView *maskView;
@property (nonatomic, strong) UIView *alertView;
@property (nonatomic, strong) UIButton *fitBtn;
@property (nonatomic, strong) NSArray *imageArr;

@end

@implementation MM_CodeResultPicView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
                
        _maskView = [[UIView alloc] init];
        _maskView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5];
        [self addSubview:_maskView];
        
        _alertView = [[UIView alloc] init];
        _alertView.backgroundColor = MM_ColorUtil(@"#ffffff");
        _alertView.layer.cornerRadius = 10;
        _alertView.layer.masksToBounds = YES;
        [self addSubview:_alertView];

        _titleLabel = [Custom_ZHTool uiLabelCreate:@"记录详情" Font:FONT_BOLD(16) TextColor:[ColorUtil colorWithHexadecimal:@"#000000"]];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.alertView addSubview:_titleLabel];

        _nameLabel = [Custom_ZHTool uiLabelCreate:@"" Font:FONT(14) TextColor:[ColorUtil colorWithHexadecimal:@"#000000"]];
        _nameLabel.numberOfLines = 0;
        [self.alertView addSubview:_nameLabel];
        
        _picView = [[UIView alloc] init];
        _picView.backgroundColor = MM_ColorUtil(@"#ffffff");
        [self.alertView addSubview:_picView];
        
        _fitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_fitBtn MM_ConfigButtonWithTitle:@"确定" titleFont:FONT_BOLD(16) titleColor:[ColorUtil colorWithHexadecimal:@"#1F71C4"] bgColor:[ColorUtil colorWithHexadecimal:@"#ffffff"] cornerRadius:10 borderColor:nil borderWidth:0];
        [_fitBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _fitBtn.tag = 101;
        [self.alertView addSubview:_fitBtn];
        
        [self setUpViews];
    }
    return self;
}

- (void)setUpViews {
        
    [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self);
    }];
    
    [self.alertView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(80);
        make.right.equalTo(-80);
        make.centerX.centerY.equalTo(self);
        make.height.equalTo(100);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(20);
        make.centerX.equalTo(self.alertView);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(12);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.centerX.equalTo(self.alertView);
    }];
       
    [self.picView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.equalTo(self.alertView);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(5);
        make.height.equalTo(40);
    }];
       
    [self.fitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.bottom.equalTo(self.alertView);
        make.height.equalTo(44);
    }];
}

- (void)setUpResonStr:(NSString *)resonStr andImageArr:(NSArray *)imageArr {
    
    self.imageArr = imageArr;
    
    CGSize nameSize = [Custom_ZHTool getTextSizeWithFont:self.nameLabel.font text:[NSString stringWithFormat:@"备注: %@",resonStr] maxWidth:ScreenW - 160];
    
    if (!stringIsEmpty(resonStr)) {
        self.nameLabel.text = [NSString stringWithFormat:@"备注: %@",resonStr];
    }
    
    CGFloat nameH = nameSize.height + 20;
    
    CGFloat space = 10;
    CGFloat picY = 10;
    CGFloat picH = 10;
        
    CGFloat relationSubViewW = ((ScreenW - max_X*2) - space*(maxCountOfRow+1))/maxCountOfRow;
    CGFloat relationSubViewH = relationSubViewW;

    NSInteger count = imageArr.count;
    
    for (UIView *subView in self.picView.subviews) {
        
        if ([subView isKindOfClass:[UIImageView class]]) {
            [subView removeFromSuperview];
        }
    }
    
    NSMutableArray *tempArr = [NSMutableArray array];
    
    if (count > 0) {
        
        for (NSInteger i=0; i<count; i++) {
            NSString *imageStr = imageArr[i];
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
            [imageView addGestureRecognizer:tap];
            imageView.tag = 10000 + i;
            [imageView MM_setImageWithUrl:imageStr placeholderImage:[UIImage imageNamed:@""]];
            imageView.frame = CGRectMake(12 + (relationSubViewW + space)*(i%6), picY+(relationSubViewH + space)*(i/6), relationSubViewW, relationSubViewH);
            [self.picView addSubview:imageView];
            
            [tempArr addObject:imageView];
        }
    }
    
    UIImageView *imageView = tempArr.lastObject;
    
    [self.picView mas_updateConstraints:^(MASConstraintMaker *make) {
       
        make.height.equalTo(CGRectGetMaxY(imageView.frame) + picH);
    }];
    
    [self.alertView mas_updateConstraints:^(MASConstraintMaker *make) {
       
//        make.left.equalTo(max_X);
//        make.right.equalTo(-max_X);
//        make.centerX.centerY.equalTo(self);
        
        make.height.equalTo(94 + nameH + CGRectGetMaxY(imageView.frame) + picH);
    }];
}

- (void)tapClick:(UITapGestureRecognizer *)tap {
    
    NSInteger index = tap.view.tag - 10000;
    
    SDPhotoBrowser *photoBrowser = [[SDPhotoBrowser alloc]init];
    photoBrowser.delegate = self;
    photoBrowser.currentImageIndex = index;
    photoBrowser.imageCount = self.imageArr.count;
    photoBrowser.sourceImagesContainerView = self.picView;
    [photoBrowser show];

}

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
    
    return [NSURL URLWithString:self.imageArr[index]];
}

- (void)btnClick:(UIButton *)btn {
    
    [self hide];
}

- (void)show {
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}
- (void)hide {
    [self removeFromSuperview];
}

@end
